<template>
  <div class="article-case">
    <div v-if="!edit" class="form-preview">
      <h2 >{{article.title}}</h2>
      <small >{{article.channel}}</small>
      <a @click="hedit" class="btn-edit" href="#">编辑</a>
    </div>
    <div v-else class="form-edit">
      <div class="form-item">
        <div class="label">标题：</div>
        <div class="input">
          <input v-model="form.title" type="text" placeholder="请输入标题" />
        </div>
      </div>
      <div class="form-item">
        <div class="label">频道：</div>
        <div class="input">
           <!-- select 的注意事项 :
           v-model 要写在select 标签上
           原理是将option的value 和变量进行双向绑定 -->
          <select v-model="form.channel">
            <option value="">请选择频道</option>
            <option value="前端">前端</option>
            <option value="运维">运维</option>
            <option value="测试">测试</option>
          </select>
        </div>
      </div>
      <div class="form-item">
        <div class="label"></div>
        <div class="input">
          <button @click="edit =false" class="cancel">取消</button>
          <button @click="hSubmit" class="submit">确认</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//思路分析
//需求1：点击编辑切换显示隐藏
//1.1定义一个edit 变量，使用v-if/ v-else 来控制俩个盒子的显示隐藏状态
//1.2给编辑按钮绑定点击事件，点击后将edit改为true
//1.3给确认取消按钮绑定点击事件，点击后将edit 改为false

// 需求2：编辑后进行数据回显 
//2.1表单数据进行双向绑定
//2.2编辑后将article 的数据赋值给form

// 需求3：
//3.1 当用户点击确定时，将form 的数据赋值给artice 
export default {
  name: 'ArticleCase',
  data() {
    return {
      edit: false,
      article: {
        title: '如何成为一名前端老鸟？',
        channel: '前端',
      },
      form: {
        title: '',
        channel: '',
      },
    };
  },
  methods: {
    hedit(){
      //1.修改编辑状态
      this.edit = true
      // 2.将artucle 里的内容复制给form 完成回显
      // this.form.title=this.article.title
      // this.form.channel=this.article.channel
 
     // 不能最直接赋值，因为俩个变量会引用同一个对象地址
     //会导致修改form 的内容会影响到article 的内容
    //  this.form =this.article 

    // 对象的拷贝
    //浅拷贝和深拷贝
    //在此处用处俩者效果一样，因为只有一层数据
    this.form={...this.article}
    },
    hSubmit(){
      this.edit = false
      //将form 的数据复制给article
      this.article = {...this.form}
    }
  },
};
</script>

<style scoped lang="less">
.form-preview {
  position: relative;
  display: flex;
  align-items: center;
}
.form-preview h2 {
  font-size: 18px;
  font-weight: normal;
}
.form-preview small {
  font-size: 12px;
  display: inline-block;
  padding: 2px 6px;
  background: #27ba9b;
  color: #fff;
  border-radius: 2px;
  transform: scale(0.8);
}
.form-preview .btn-edit {
  font-size: 12px;
  display: none;
  text-decoration: none;
  color: #069;
  margin-left: 20px;
}
.form-preview:hover .btn-edit {
  display: block;
}
.form-edit {
  padding-top: 20px;
}
.form-item {
  display: flex;
  margin-bottom: 20px;
  align-items: center;
}
.form-item .label {
  width: 60px;
  text-align: right;
  font-size: 14px;
}
.form-item .input {
  flex: 1;
}
.form-item input,
.form-item select {
  appearance: none;
  outline: none;
  border: 1px solid #ccc;
  width: 200px;
  height: 40px;
  box-sizing: border-box;
  padding: 10px;
  color: #666;
}
.form-item input::placeholder {
  color: #666;
}
.form-item .cancel,
.form-item .submit {
  appearance: none;
  outline: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 10px;
  margin-right: 10px;
  font-size: 12px;
  background: #ccc;
}
.form-item .submit {
  border-color: #069;
  background: #069;
  color: #fff;
}
</style>